package com.zyb.springcloud.controller;

import com.zyb.springcloud.entities.CommonResult;
import com.zyb.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderController {

    @Resource
    private RestTemplate restTemplate;

    private static final String URL_PATH="http://cloud-payment-service";

    @GetMapping(value="/consumer/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable Long id){
        return restTemplate.getForObject(URL_PATH + "/payment/get/"+id,CommonResult.class,id);
    }

    @GetMapping(value="/consumer/payment/create")
    public CommonResult createPayment(Payment payment){
        return restTemplate.postForObject(URL_PATH + "/payment/create",payment,CommonResult.class);
    }

    /**
     * getForEntity:返回对象为ResponseEntity对象，包含了响应中的一些重要信息，比如响应头、响应状态码、响应体等
     * getForObject：返回对象为响应体中数据转化成的对象，基本上可以理解为Json
     * @param id
     * @return
     */
    @GetMapping(value="/consumer/payment/entity/get/{id}")
    public CommonResult getPaymentById4Entity(@PathVariable Long id){
        ResponseEntity<CommonResult>  resultResponseEntity= restTemplate.getForEntity(URL_PATH + "/payment/get/"+id,CommonResult.class,id);
        if(resultResponseEntity.getStatusCode().is2xxSuccessful()){
            return  resultResponseEntity.getBody();
        }else{
            return new CommonResult(resultResponseEntity.getStatusCode().value(),"查询失败~~");
        }
    }

}
